package com.tanhua.server.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.dubbo.config.annotation.Reference;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tanhua.common.enums.SexEnum;
import com.tanhua.common.mapper.BlacklistMapper;
import com.tanhua.common.mapper.UserInfoMapper;
import com.tanhua.common.pojo.Blacklist;
import com.tanhua.common.pojo.User;
import com.tanhua.common.pojo.UserInfo;
import com.tanhua.common.utils.UserThreadLocal;
import com.tanhua.common.vo.PageResult;
import com.tanhua.dubbo.server.api.UsersApi;
import com.tanhua.dubbo.server.pojo.BlacklistedUsers;
import com.tanhua.dubbo.server.pojo.SaveUserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;


/**
 * @author LOVE
 */
@Service
public class UsersService {

    @Reference
    private UsersApi usersApi;

    @Autowired
    private UserInfoMapper userInfoMapper;

    @Autowired
    private BlacklistMapper blacklistMapper;


    /**
     * 获取用户信息
     *
     * @param userID
     * @param huanxinID
     */
    public SaveUserInfo getUserInformation(String userID, String huanxinID) {
        if (userID != null || huanxinID != null) {
            // TODO: 2021/7/11
        }
        User user = UserThreadLocal.get();
        UserInfo userInfo = userInfoMapper.selectById(user.getId());
        SaveUserInfo saveUserInfo = new SaveUserInfo();
        BeanUtil.copyProperties(userInfo, saveUserInfo, "gender", "id", "marriage");
        saveUserInfo.setGender(userInfo.getSex().equals("男") ? "man" : "woman");
        saveUserInfo.setId(userInfo.getId().intValue());
        saveUserInfo.setMarriage(userInfo.getMarriage().equals("1") ? 1 : 0);
        return saveUserInfo;
    }

    /**
     * 保存用户信息
     *
     * @param saveUserInfo 用户信息
     * @return 是否成功
     */
    public Boolean saveUserInformation(SaveUserInfo saveUserInfo) {
        try {
            //获取信息
            User user = UserThreadLocal.get();
            //获取原始数据
            UserInfo userInfo = userInfoMapper.selectById(user.getId());
            //将要修改的数据传到userinfo中
            BeanUtil.copyProperties(saveUserInfo, userInfo, "gender", "id", "marriage");
            //获取直接的id
            userInfo.setId(user.getId());
            //设置性别
            if (saveUserInfo.getGender() != null) {
                userInfo.setSex(StrUtil.equals(saveUserInfo.getGender(), "man") ? SexEnum.MAN : SexEnum.WOMAN);
            }
            //设置婚姻状况
            if (saveUserInfo.getMarriage() != null) {
                userInfo.setMarriage(saveUserInfo.getMarriage() == 1 ? "1" : "0");
            }

            //去数据库进行数据修改
            userInfoMapper.updateById(userInfo);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    /**
     * 黑名单列表
     *
     * @param page
     * @param pagesize
     */
    public PageResult blacklist(Integer page, Integer pagesize) {
        try {
            List<BlacklistedUsers> list = null;
            //获取用户id
            User user = UserThreadLocal.get();

            //根据id查询黑名单信息
            PageRequest of = PageRequest.of(page, pagesize);
            List<Blacklist> blacklists = blacklistMapper.selectList(new QueryWrapper<Blacklist>().eq("user_id", user.getId()));
            //查询黑名单用户详细信息
            List<Object> blackUserId = CollUtil.getFieldValues(blacklists, "blackUserId");
            //获取黑名单的用户的信息
            List<UserInfo> userId = userInfoMapper.selectList(new QueryWrapper<UserInfo>().in("user_id", blackUserId));
            //用键值对的形式装起来(可省略)
            Map<Long, UserInfo> userInfoMap = userId.stream().collect(Collectors.toMap(UserInfo::getUserId, Function.identity()));

            //数据封装
            BlacklistedUsers blacklistedUsers;
            list = new ArrayList<>();
            for (Blacklist blacklist : blacklists) {
                blacklistedUsers = new BlacklistedUsers();
                //id
                blacklistedUsers.setId(blacklist.getBlackUserId().intValue());
                //头像
                blacklistedUsers.setAvatar(userInfoMap.get(blacklistedUsers.getId().longValue()).getLogo());
                //昵称
                blacklistedUsers.setNickname(userInfoMap.get(blacklistedUsers.getId().longValue()).getNickName());
                //性别
                blacklistedUsers.setGender(userInfoMap.get(blacklistedUsers.getId().longValue()).getSex().getValue() == 1 ? "男" : "女");
                //年龄
                blacklistedUsers.setAge(userInfoMap.get(blacklistedUsers.getId().longValue()).getAge());

                //添加到集合
                list.add(blacklistedUsers);
            }
            //封装数据并返回
            return new PageResult(100, pagesize, 100, page, list);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 取消黑名单
     *
     * @param toUserId 被拉黑的用户id
     * @return 是否成功
     */
    public Boolean removeBlacklist(String toUserId) {
        int user_id = blacklistMapper.delete(new QueryWrapper<Blacklist>().eq("black_user_id", toUserId));
        if (user_id != 1) {
            return false;
        }
        return true;
    }
}
